﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Duckbill
{
    public class Enemy : GameObject
    {
        protected static float gravity = 1180f;
        protected static float maxFallingSpeed = 900f;

        public int health = 10;

        protected Vector2 vel = Vector2.Zero;
        protected float speed = 100f;
        protected bool onGround = false;
        protected float jumping = 0.0f;
        protected float time = 0f;

        public Enemy(string sprite, int health, Vector2 pos)
            : base(sprite, pos)
        {
            type = ObjectType.enemy;
            this.health = health;
        }

        protected void DoGravity(float time)
        {
            if (!onGround)
            {
                vel.Y += gravity * time;
                vel.Y = Math.Min(vel.Y, maxFallingSpeed);
            }
        }

        protected virtual void CollisionLogic(float time, Dictionary<ObjectType, List<GameObject>> others)
        {
            int iterations = (int)(vel.Length()/Math.Min(sprite.size.X, sprite.size.Y))+1;
            for (int i = 0; i < iterations; i++)
            {
                int x = (int)(pos.X + vel.X*time*(i+1)/iterations);
                int y = (int)(pos.Y + vel.Y*time*(i+1)/iterations);
                int w = (int)sprite.size.X;
                int h = (int)sprite.size.Y;

                onGround = false;
                foreach (Tile tile in others[ObjectType.tile])
                {
                    if (tile.tileType == TileType.solid)
                    {
                        if (!(Rectangle.Intersect(new Rectangle(x + w*7/8, y + h/4, w/8 + 1, h/2), tile.getRect()).IsEmpty) && vel.X > 0) //tile to the right
                        {
                            pos.X = x = (int)tile.pos.X - w;
                            vel.X = 0;
                        }
                        if (!(Rectangle.Intersect(new Rectangle(x - 1, y + h/4, w/8 + 1, h/2), tile.getRect()).IsEmpty) && vel.X < 0) //tile to the left
                        {
                            pos.X = x = (int)(tile.pos.X + tile.sprite.size.X);
                            vel.X = 0;
                        }
                        if (!Rectangle.Intersect(new Rectangle(x + w/4, y, w/2, h/8), tile.getRect()).IsEmpty && vel.Y < 0) //tile above player
                        {
                            pos.Y = (int)(tile.pos.Y + tile.sprite.size.Y);
                            vel.Y *= -0.2f;
                            jumping = 0f;
                            y = (int)(pos.Y + vel.Y*time*(i+1)/iterations);
                        }
                        if (!(Rectangle.Intersect(new Rectangle(x + w/4, y + h*7/8, w/2, h/8 + 1), tile.getRect()).IsEmpty) && vel.Y >= 0) //tile beneath player
                        {
                            onGround = true;
                            pos.Y = y = (int)tile.pos.Y - h;
                            vel.Y = 0;
                        }
                    }
                }
            }
        }

        public override void Update(float time, Dictionary<ObjectType, List<GameObject>> others)
        {
            this.time += time;
            vel.X = speed*(2*((int)this.time%2)-1);
            
            DoGravity(time);
            CollisionLogic(time, others);
        }
        public override void Move(float time)
        {
            pos += vel*time;
        }
    }
}
